package com.authine.cloudpivot.web.api.secondopen.service.impl;

import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import com.authine.cloudpivot.web.api.secondopen.dao.NewsMapper;
import com.authine.cloudpivot.web.api.secondopen.domain.AttachModel;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsModel;
import com.authine.cloudpivot.web.api.secondopen.domain.NewsTypeModel;
import com.authine.cloudpivot.web.api.secondopen.domain.RemarkModel;
import com.authine.cloudpivot.web.api.secondopen.service.NewsService;
import com.authine.cloudpivot.web.api.secondopen.utils.CommUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class NewsServiceImpl implements NewsService {

    @Autowired
    private CommUtil commUtil;
    @Autowired
    private NewsMapper newsMapper;
    @Autowired
    private DubboConfigService dubboConfigService;

    @Override
    public PageInfo<NewsModel> getNewsList(int startPage, int pageSize, String newsType, String title, String userId, String tableName, boolean count) {
        //获取当前用户的所有上级部门,包括本身
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        Page page = PageHelper.startPage(startPage, pageSize, true);
        page.setOrderBy("top DESC ,publishTime DESC");
        List<NewsModel> list = newsMapper.selectNewsList(tableName, newsType, title, org);
        PageInfo<NewsModel> pageInfo = new PageInfo<NewsModel>(list);
        return pageInfo;
    }

    /**
     * 获取已阅数量,点赞数量
     *
     * @param type
     * @param bizObjectId
     * @return
     */
    @Override
    public int getReadNum(String type, String bizObjectId, String userId, String tableName) {
        return newsMapper.selectCount(tableName, type, bizObjectId, userId);
    }

    /**
     * 获取评论数量
     *
     * @param bizObjectId
     * @param tableName
     * @return
     */
    @Override
    public int getComentNum(String bizObjectId, String tableName) {
        return newsMapper.selectComentCount(tableName, bizObjectId);
    }
    /**
     * 是否已读,点赞
     *
     * @param type
     * @param bizObjectId
     * @param userId
     * @return
     */
    @Override
    public Boolean getFlags(String type, String bizObjectId, String userId, String tableName) {
        int num = newsMapper.selectCount(tableName, type, bizObjectId, userId);
        return num > 0 ? true : false;
    }

    @Override
    public PageInfo<NewsModel> getNewsList2(int startPage, int pageSize, String newsType, String title, Map<String,String> sort, String userId, String tableName, boolean count, String isImportant, String fromTime, String toTime){
        //获取当前用户的所有上级部门,包括本身
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        Page page = PageHelper.startPage(startPage, pageSize, true);
        //新增排序author，title，publishTime
        String checkStr="author,title,publishTime";
        String sortStr="";
        if(sort==null||sort.isEmpty()){
            sortStr="top DESC ,publishTime DESC";
        }else{
            for(String key:sort.keySet()){
                if(checkStr.contains(key)){
                    if(sortStr.equals("")){
                        if(StringUtils.isBlank(newsType)&&"author".equals(key)){
                            sortStr+="creater"+((sort.get(key).equals("asc"))?" asc":" desc");
                        }else{
                            sortStr+=key+((sort.get(key).equals("asc"))?" asc":" desc");
                        }
                    }else{
                        if(StringUtils.isBlank(newsType)&&"author".equals(key)){
                            sortStr+="creater"+((sort.get(key).equals("asc"))?" asc":" desc");
                        }else {
                            sortStr+=","+key+((sort.get(key).equals("asc"))?" asc":" desc");
                        }

                    }
                }
            }
        }
        page.setOrderBy(sortStr);
        List<NewsModel> list = newsMapper.selectNewsList2(tableName, newsType, title, org, isImportant, fromTime, toTime);
        PageInfo<NewsModel> pageInfo = new PageInfo<NewsModel>(list);
        return pageInfo;
    }

    /**
     * 通过id获取详细新闻
     *
     * @param id
     * @return
     */
    @Override
    public NewsModel getNewsById(String id, String tableName) {
        return newsMapper.selectNewsById(tableName, id);
    }
    @Override
    public NewsModel getNewsById2(String id, String tableName) {
        return newsMapper.selectNewsById2(tableName, id);
    }


    /**
     * 获取新闻附件
     * vb
     *
     * @param bizObjectId 业务表objectId
     * @return
     */
    @Override
    public List<AttachModel> getAttchList(String bizObjectId) {
        return newsMapper.selectAttchByBizId(bizObjectId);
    }

    /**
     * 获取评论列表
     *
     * @return
     */
    @Override
    public List<RemarkModel> getRemarkList(String userId, String bizObjectId, String tableName) {
        return newsMapper.selectRemarkByBizId(userId, tableName, bizObjectId);
    }

    /**
     * 获取上一页
     * @param ifTop
     * @param userId
     * @param publishTime
     * @param newsType
     * @param tableName
     * @return
     */
    @Override
    public String getPreviousId(boolean ifTop, String userId, Date publishTime, String newsType, String tableName) {
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        String id = null;
        if (ifTop) {
            NewsModel model = newsMapper.selectTop(tableName, newsType, ">", publishTime, org, "1", "ASC");
            if (model != null) {
                id = model.getId();
            }
        } else {
            NewsModel model = newsMapper.selectNomal(tableName, newsType, ">", publishTime, org, "0", "ASC");
            if (model != null) {
                id = model.getId();
            } else {
                NewsModel model1 = newsMapper.selectTopMin(tableName, newsType, org, "1", "ASC");
                if (model1 != null) {
                    id = model1.getId();
                }
            }
        }
        return id;
    }

    /**
     * 获取下一页
     * @param ifTop
     * @param userId
     * @param publishTime
     * @param newsType
     * @param tableName
     * @return
     */
    @Override
    public String getNextId(boolean ifTop, String userId, Date publishTime, String newsType, String tableName) {
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        String id = null;
        if (ifTop) {
            NewsModel model = newsMapper.selectNomal(tableName, newsType, "<", publishTime, org, "1", "DESC");
            if (model != null) {
                id = model.getId();
            } else {
                NewsModel model1 = newsMapper.selectNomalMax(tableName, newsType, org, "0", "DESC");
                if (model1 != null) {
                    id = model1.getId();
                }
            }
        } else {
            NewsModel model = newsMapper.selectTop(tableName, newsType, "<", publishTime, org, "0", "DESC");
            if (model != null) {
                id = model.getId();
            }
        }
        return id;
    }

    /**
     * 点赞
     * @param id
     * @param newsType
     * @param schemaCode
     * @param userId
     * @param type
     * @return
     */
    @Override
    public String setNumber(String id, String newsType, String schemaCode, String userId, String type) {
        String tableName=dubboConfigService.getBizObjectFacade().getTableName(schemaCode);
        int count=newsMapper.deleteNumber(id, newsType,tableName,  userId, type);
        if(count==0){
            Map<String, Object> map = new HashMap<>();
            map.put("bizObjectId", id);
            map.put("recordType", type);
            map.put("newsType", newsType);
            BizObjectModel bo = new BizObjectModel(schemaCode, map, Boolean.FALSE);
            String objectId = dubboConfigService.getBizObjectFacade().saveBizObject(userId, bo, Boolean.FALSE);
            return objectId;
        }
        return "delete";
    }

    /**
     * 获取未评论审批列表
     *
     * @param tableName
     * @return
     */
    @Override
    public PageInfo<RemarkModel> getNewsRemarkList(int startPage, int pageSize, String tableName,String bizObjectId,String userId) {
        Page page = PageHelper.startPage(startPage, pageSize, true);
        page.setOrderBy("createdTime DESC");
        List<RemarkModel> list = newsMapper.selectNewsRemarkList(tableName,bizObjectId,userId);
        PageInfo<RemarkModel> pageInfo = new PageInfo<RemarkModel>(list);
        return pageInfo;
    }

    /**
     * 获取新闻中心目录
     *
     * @return
     */
    @Override
    public List<NewsTypeModel> getNewsTypeList(String tableName) {
        return newsMapper.selectNewsType();
    }

    /**
     * 获取未读新闻条数
     *
     * @param newsType
     * @param userId
     * @param tableName
     * @return
     */
    @Override
    public int getTypeUnReadNum(String newsType, String userId, String tableName) {
        String RECORD = dubboConfigService.getBizObjectFacade().getTableName("record");

        //获取当前用户的所有上级部门,包括本身
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        int total = newsMapper.selectNewsListCount(tableName, newsType, org);

        int readCount = newsMapper.selectReadCount(RECORD, newsType, userId);
        return total- readCount;

    }

    @Override
    public PageInfo<NewsModel> getNewsListByDeptIds(int startPage, int pageSize, String newsType, String title, List<String> deptIds, String tableName,boolean count) {
        //获取当前用户的所有上级部门,包括本身
        List<String> org = deptIds;
        Page page = PageHelper.startPage(startPage, pageSize, true);
        page.setOrderBy("top DESC ,publishTime DESC");
        List<NewsModel> list = newsMapper.selectNewsListByDept(tableName, newsType, title, org);
        PageInfo<NewsModel> pageInfo = new PageInfo<NewsModel>(list);
        return pageInfo;
    }

    /**
     * 获取未评论审批列表
     *
     * @param tableName
     * @return
     */
    @Override
    public PageInfo<RemarkModel> getUnRemarkList(int startPage, int pageSize, String tableName) {
        Page page = PageHelper.startPage(startPage, pageSize, true);
        page.setOrderBy("createdTime DESC");
        List<RemarkModel> list = newsMapper.selectRemarkList(tableName);
        PageInfo<RemarkModel> pageInfo = new PageInfo<RemarkModel>(list);
        return pageInfo;
    }

    /**
     * 获取集团图片新闻
     *
     * @return
     */
    @Override
    public PageInfo<NewsModel> getImageNewsList(int startPage, int pageSize, String newsType, String userId, String tableName) {
        //获取当前用户的所有上级部门,包括本身
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        Page page = PageHelper.startPage(startPage, pageSize, false);
        page.setOrderBy("top DESC ,publishTime DESC");
        List<NewsModel> list = newsMapper.selectImageNewsList(tableName, newsType, org);
        PageInfo<NewsModel> pageInfo = new PageInfo<NewsModel>(list);
        return pageInfo;
    }


    @Override
    public PageInfo<NewsModel> getIndexNewsList(int startPage, int pageSize, String newsType, String title, String userId, String tableName, boolean count) {
        //获取当前用户的所有上级部门,包括本身
        List<String> org = commUtil.GetDpeptsByUserId(userId);
        Page page = PageHelper.startPage(startPage, pageSize, true);
        page.setOrderBy("top DESC ,publishTime DESC");
        List<NewsModel> list = newsMapper.selectIndexNewsList(tableName, newsType, title, org);
        PageInfo<NewsModel> pageInfo = new PageInfo<NewsModel>(list);
        return pageInfo;
    }
    /**
     * 获取图片列表
     *
     * @param bizObjectId
     * @return
     */
    @Override
    public List<AttachModel> getPicList(String bizObjectId) {
        return newsMapper.selectPicByBizId(bizObjectId);
    }



}
